package com.yili.oa.service.impl;

import com.yili.oa.dao.NoticeMapper;
import com.yili.oa.entity.SearchBase;
import com.yili.oa.models.Notice;
import com.yili.oa.models.NoticeExample;
import com.yili.oa.service.NoticeService;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.utils.annotate.DataBase;
import com.yili.oa.vo.request.NoticeReq;
import com.yili.oa.vo.response.NoticeRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/7/31 0031
 * @email 200890949@qq.com
 */
@SuppressWarnings("ALL")
@Service
@DataBase("lyt_game_db")
@Transactional(value = "transactionManager")
public class NoticeSvImpl implements NoticeService {

    @Autowired
    private NoticeMapper mapper;

    @Override
    public List<Notice> getList(SearchBase searchBase) {

        NoticeExample example = new NoticeExample();
        NoticeExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(searchBase.getId())){
            criteria.andIdEqualTo(Long.valueOf(searchBase.getId()));
        }
        return mapper.selectByExample(example);
    }

    @Override
    public void add(NoticeReq noticeReq) {
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeReq,notice);
        notice.setCreateTime(new Date());
        try {
            mapper.insert(notice);
        }catch (Exception e){
            throw new AjaxException("添加失败");
        }
    }

    @Override
    public void delNotice(List<Long> list) {
        NoticeExample example = new NoticeExample();
        NoticeExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        try {
            mapper.deleteByExample(example);
        }catch (Exception e){
            throw new AjaxException("删除失败");
        }
    }

    @Override
    public void update(NoticeReq noticeReq) {
        Notice notice = mapper.selectByPrimaryKey(noticeReq.getId());
        BeanUtils.copyProperties(noticeReq, notice);
        mapper.updateByPrimaryKeySelective(notice);
    }

    @Override
    public List<NoticeRes> getByType(Integer type) {
        NoticeExample example = new NoticeExample();
        NoticeExample.Criteria criteria = example.createCriteria();
        criteria.andNoticeTypeEqualTo(type);
        List<Notice> list = mapper.selectByExample(example);
        List<NoticeRes> result = new ArrayList<>();
        for (Notice notice:
             list) {
            NoticeRes noticeRes = new NoticeRes();
            BeanUtils.copyProperties(notice,noticeRes);
            if (notice.getNoticeType().equals(0)){
                noticeRes.setNoticeType("浮动公告");
            }else if(notice.getNoticeType().equals(1)){
                noticeRes.setNoticeType("日常消息");
            }else {
                noticeRes.setNoticeType("游戏帮助");
            }
            result.add(noticeRes);
        }
        if (ListUtils.isNullOrEmpty(result)){
            return null;
        }
        return result;
    }
}
